/* main menu */
var yukon_main_menu = {
    init: function () {

        // add '.has_submenu' class if section has childrens
        $('#main_menu ul > li').each(function () {
            if ($(this).children('ul').length) {
                $(this).addClass('has_submenu');
            }
        });

        // accordion menu
        $(document).off('click', '.side_menu_expanded #main_menu .has_submenu > a').on('click', '.side_menu_expanded #main_menu .has_submenu > a', function () {
            if($(this).parent('.has_submenu').hasClass('first_level')) {
                var $this_parent = $(this).parent('.has_submenu'),
                    panel_active = $this_parent.hasClass('section_active');

                if (!panel_active) {
                    $this_parent.siblings().removeClass('section_active').children('ul').slideUp('200');
                    $this_parent.addClass('section_active').children('ul').slideDown('200');
                } else {
                    $this_parent.removeClass('section_active').children('ul').slideUp('200');
                }
            } else {
                var $submenu_parent = $(this).parent('.has_submenu'),
                    submenu_active = $submenu_parent.hasClass('submenu_active');

                if (!submenu_active) {
                    $submenu_parent.siblings().removeClass('submenu_active').children('ul').slideUp('200');
                    $submenu_parent.addClass('submenu_active').children('ul').slideDown('200');
                } else {
                    $submenu_parent.removeClass('submenu_active').children('ul').slideUp('200');
                }
            }
        });

        // side menu initialization
        if(!$('#main_menu .has_submenu').hasClass('section_active')) {
            $('#main_menu .has_submenu .act_nav').closest('.has_submenu').children('a').click();
        } else {
            $('#main_menu .has_submenu.section_active').children('ul').show();
        }

        $('.menu_toggle').on("click",function() {
            if($('body').hasClass('side_menu_expanded')) {
                yukon_main_menu.menu_collapse();
            } else if($('body').hasClass('side_menu_collapsed')) {
                yukon_main_menu.menu_expand();
            }
            $(window).off("debouncedresize").trigger('resize').on("debouncedresize");
        });

        // collapse navigation on mobile devices
        if($('body').hasClass('side_menu_expanded') && $(window).width() <= 992 ) {
            yukon_main_menu.menu_collapse();
        }

        // create scrollbar if menu is expanded
        if($('body').hasClass('side_menu_expanded')) {
            yukon_main_menu.menu_scrollbar_create();
        }

        // uncomment function bellow to activate saving side menu states
        yukon_main_menu.menu_cookie();

    },
    menu_expand: function() {
        $('body').addClass('side_menu_expanded').removeClass('side_menu_collapsed');
        $('.menu_toggle').find('.toggle_left').show();
        $('.menu_toggle').find('.toggle_right').hide();
        yukon_main_menu.menu_scrollbar_create();
    },
    menu_collapse: function() {
        $('body').removeClass('side_menu_expanded').addClass('side_menu_collapsed');
        $('.menu_toggle').find('.toggle_left').hide();
        $('.menu_toggle').find('.toggle_right').show();
        yukon_main_menu.menu_scrollbar_destroy();
    },
    menu_cookie: function() {
        $('.menu_toggle').on('click',function() {
            if($('body').hasClass('side_menu_expanded')) {
                $.cookie('side_menu', '1');
            } else if($('body').hasClass('side_menu_collapsed')) {
                $.cookie('side_menu', '0');
            }
        });

        var $side_menu_cookie = $.cookie('side_menu');

        if($side_menu_cookie != undefined) {
            if($side_menu_cookie == '1') {
                yukon_main_menu.menu_expand();
            } else if($side_menu_cookie == '0') {
                yukon_main_menu.menu_collapse();
            }
        }
    },
    menu_scrollbar_create: function() {
        $("#main_menu .menu_wrapper").mCustomScrollbar({
            theme: "minimal-dark",
            scrollbarPosition: "outside"
        });
    },
    menu_scrollbar_destroy: function() {
        $("#main_menu .menu_wrapper").mCustomScrollbar('destroy');
    }
};
function launchFullScreen(element) {
    var request;
    if (!$('body').hasClass("full-screen")) {
        request = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen ||	element.msRequestFullScreen;
        if(typeof request!="undefined" && request){
            request.call(element);
        }else{
            alert("你的浏览器不支持，请按F11进行全屏预览");
            return false;
        }
    } else {
        request = document.cancelFullScreen|| document.webkitCancelFullScreen || document.mozCancelFullScreen || document.msCancelFullScreen || document.exitFullscreen;
        if(typeof request!="undefined" && request){
            request.call(document);
        }
    }
    $('body').toggleClass("full-screen");
}

$(function(){
    yukon_main_menu.init();
    FastClick.attach(document.body);
});

function dateFormatter(value, dateformat) {
    if (typeof(dateformat) == "undefined"||typeof(dateformat) == "object") {
        dateformat = "YYYY-MM-DD";
    }
    return moment(value).format(dateformat);
}